/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaces;

import conexion.Operaciones;
import java.awt.CardLayout;
import javax.swing.JOptionPane;

/**
 *
 * @author vikko
 */
public class PanelUsuarios extends javax.swing.JPanel {

    /**
     * Creates new form PanelUsuarios
     */
    public PanelUsuarios() {
        initComponents();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        panel_altaUsuario = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tfield_nombreUsuario = new javax.swing.JTextField();
        combo_tipoUsuario = new javax.swing.JComboBox();
        pfield_password = new javax.swing.JPasswordField();
        pfield_confirmaPassword = new javax.swing.JPasswordField();
        btn_registrarUsuario = new javax.swing.JButton();
        btn_cancelarRegistroUsuario = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        panel_consultaUsuario = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla_usuarios = new javax.swing.JTable();
        btn_mostrarUsuarios = new javax.swing.JButton();
        btn_buscarUsuario = new javax.swing.JButton();
        btn_eliminar = new javax.swing.JButton();
        btn_modificaUsuario = new javax.swing.JButton();
        panelBotones = new javax.swing.JPanel();
        btn_salirUsuario = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(800, 579));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/pc_banner2.png"))); // NOI18N

        panel_altaUsuario.setBorder(javax.swing.BorderFactory.createTitledBorder("Dar de alta un nuevo usuario"));

        jLabel2.setText("Nombre de usuario:");

        jLabel3.setText("Password:");

        jLabel4.setText("Confirmar password:");

        jLabel5.setText("Tipo de usuario:");

        combo_tipoUsuario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Usuario", "Administrador" }));

        btn_registrarUsuario.setText("Registrar");
        btn_registrarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_registrarUsuarioActionPerformed(evt);
            }
        });

        btn_cancelarRegistroUsuario.setText("Cancelar");
        btn_cancelarRegistroUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelarRegistroUsuarioActionPerformed(evt);
            }
        });

        jLabel6.setText("Privilegios:");

        jLabel7.setText("El tipo \"Usuario\" tendrá privilegios restringidos dentro del sistema.");

        jLabel8.setText("El tipo \"Administrador\" tendrá todos los privilegios en el sistema.");

        javax.swing.GroupLayout panel_altaUsuarioLayout = new javax.swing.GroupLayout(panel_altaUsuario);
        panel_altaUsuario.setLayout(panel_altaUsuarioLayout);
        panel_altaUsuarioLayout.setHorizontalGroup(
            panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_altaUsuarioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_altaUsuarioLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btn_cancelarRegistroUsuario)
                        .addGap(18, 18, 18)
                        .addComponent(btn_registrarUsuario))
                    .addGroup(panel_altaUsuarioLayout.createSequentialGroup()
                        .addGroup(panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panel_altaUsuarioLayout.createSequentialGroup()
                                .addGroup(panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addGap(18, 18, 18)
                                .addGroup(panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(tfield_nombreUsuario)
                                    .addComponent(combo_tipoUsuario, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(pfield_password)
                                    .addComponent(pfield_confirmaPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)))
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(6, 6, 6))
        );
        panel_altaUsuarioLayout.setVerticalGroup(
            panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_altaUsuarioLayout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfield_nombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(pfield_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(pfield_confirmaPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(combo_tipoUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panel_altaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_registrarUsuario)
                    .addComponent(btn_cancelarRegistroUsuario))
                .addGap(27, 27, 27)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8))
        );

        panel_consultaUsuario.setBorder(javax.swing.BorderFactory.createTitledBorder("Usuarios registrados"));

        jLabel9.setText("Los Usuarios registrados, su Password y Tipo de usuario son:");

        tabla_usuarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tabla_usuarios);

        btn_mostrarUsuarios.setText("Mostrar todos");
        btn_mostrarUsuarios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_mostrarUsuariosActionPerformed(evt);
            }
        });

        btn_buscarUsuario.setText("Buscar usuario");
        btn_buscarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_buscarUsuarioActionPerformed(evt);
            }
        });

        btn_eliminar.setText("Eliminar usuario");
        btn_eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_eliminarActionPerformed(evt);
            }
        });

        btn_modificaUsuario.setText("Modificar usuario");
        btn_modificaUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_modificaUsuarioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panel_consultaUsuarioLayout = new javax.swing.GroupLayout(panel_consultaUsuario);
        panel_consultaUsuario.setLayout(panel_consultaUsuarioLayout);
        panel_consultaUsuarioLayout.setHorizontalGroup(
            panel_consultaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_consultaUsuarioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panel_consultaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel_consultaUsuarioLayout.createSequentialGroup()
                        .addComponent(btn_buscarUsuario)
                        .addGap(18, 18, 18)
                        .addComponent(btn_mostrarUsuarios, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btn_eliminar))
                    .addGroup(panel_consultaUsuarioLayout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_consultaUsuarioLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btn_modificaUsuario)))
                .addContainerGap())
        );
        panel_consultaUsuarioLayout.setVerticalGroup(
            panel_consultaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel_consultaUsuarioLayout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(panel_consultaUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_mostrarUsuarios)
                    .addComponent(btn_buscarUsuario)
                    .addComponent(btn_eliminar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btn_modificaUsuario)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btn_salirUsuario.setText("Salir");
        btn_salirUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_salirUsuarioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelBotonesLayout = new javax.swing.GroupLayout(panelBotones);
        panelBotones.setLayout(panelBotonesLayout);
        panelBotonesLayout.setHorizontalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBotonesLayout.createSequentialGroup()
                .addContainerGap(313, Short.MAX_VALUE)
                .addComponent(btn_salirUsuario)
                .addContainerGap())
        );
        panelBotonesLayout.setVerticalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBotonesLayout.createSequentialGroup()
                .addGap(0, 22, Short.MAX_VALUE)
                .addComponent(btn_salirUsuario))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel_altaUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelBotones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(panel_consultaUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panel_consultaUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(panelBotones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(panel_altaUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(13, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btn_registrarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_registrarUsuarioActionPerformed
        String usuario= tfield_nombreUsuario.getText();
        String contra = pfield_password.getText();
        String confirma_contra= pfield_confirmaPassword.getText();
        String tipo= combo_tipoUsuario.getSelectedItem().toString();
        if (contra.compareTo(confirma_contra)!=0){
            JOptionPane.showMessageDialog(null, "Las contraseñas no coinciden","Error en contraseñas",JOptionPane.ERROR_MESSAGE);
            pfield_password.setText("");
            pfield_confirmaPassword.setText("");            
        }
        else{            
            Operaciones operacion = new Operaciones();
            int resConsulta= operacion.altaUsuario(usuario, contra, tipo);         
            if (resConsulta==1)  //Verificación de que el usuario no exista en la BD.
                JOptionPane.showMessageDialog(null, "El usuario \""+usuario+"\" fue dado de alta exitosamente", "Usuario registrado", JOptionPane.INFORMATION_MESSAGE);
            else if (resConsulta==-1)
                JOptionPane.showMessageDialog(null, "El usuario \""+usuario+"\" ya existe, por favor seleccione otro Nombre de Usuario.", "Error en registro", JOptionPane.ERROR_MESSAGE);            
            tfield_nombreUsuario.setText("");
            pfield_password.setText("");
            pfield_confirmaPassword.setText("");
        }    
    }//GEN-LAST:event_btn_registrarUsuarioActionPerformed

    private void btn_cancelarRegistroUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelarRegistroUsuarioActionPerformed
        tfield_nombreUsuario.setText("");
        pfield_password.setText("");
        pfield_confirmaPassword.setText("");
    }//GEN-LAST:event_btn_cancelarRegistroUsuarioActionPerformed

    private void btn_mostrarUsuariosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_mostrarUsuariosActionPerformed
        muestraUsuarios();
    }//GEN-LAST:event_btn_mostrarUsuariosActionPerformed

    private void btn_buscarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_buscarUsuarioActionPerformed
        String usuario= JOptionPane.showInputDialog("Ingresa el Nombre de usuario a buscar");
        buscaUsuario(usuario);
    }//GEN-LAST:event_btn_buscarUsuarioActionPerformed

    private void btn_eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_eliminarActionPerformed
        String usuario= JOptionPane.showInputDialog("Ingresa el Nombre de usuario a eliminar");
        eliminarUsuario(usuario);
    }//GEN-LAST:event_btn_eliminarActionPerformed

    private void btn_salirUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_salirUsuarioActionPerformed
        int res= JOptionPane.showConfirmDialog(null, "¿Realmente desea salir del sistema?");     
        if (res==0)
            System.exit(0);
    }//GEN-LAST:event_btn_salirUsuarioActionPerformed

    private void btn_modificaUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_modificaUsuarioActionPerformed
        String usuario= JOptionPane.showInputDialog("Ingresa el Nombre de usuario a modificar");
        String []datos= modificaUsuario(usuario);
      //  System.out.println("Nombre: "+datos[0]);
      //  System.out.println("Contraseña: "+datos[1]);
      //  System.out.println("Password: "+datos[2]);
        FrameModificaUsuario modifica= new FrameModificaUsuario(datos [0], datos[1], datos[2]);
        modifica.setVisible(true);        
    }//GEN-LAST:event_btn_modificaUsuarioActionPerformed
    
    public void muestraUsuarios(){
       Operaciones oper= new Operaciones();
       tabla_usuarios.setModel(oper.consultaTodosUsuarios());     
    }
    
    public void buscaUsuario(String user){
        Operaciones oper= new Operaciones();
       tabla_usuarios.setModel(oper.buscaUsuario(user));
    }
    
    public String[] modificaUsuario(String user){
        Operaciones oper= new Operaciones();
        String []datos= oper.modificarUsuario(user);
        return datos;
    }
    
    public void eliminarUsuario(String user){
        Operaciones oper= new Operaciones();
        oper.eliminaUsuario(user);
       tabla_usuarios.setModel(oper.consultaTodosUsuarios());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_buscarUsuario;
    private javax.swing.JButton btn_cancelarRegistroUsuario;
    private javax.swing.JButton btn_eliminar;
    private javax.swing.JButton btn_modificaUsuario;
    private javax.swing.JButton btn_mostrarUsuarios;
    private javax.swing.JButton btn_registrarUsuario;
    private javax.swing.JButton btn_salirUsuario;
    private javax.swing.JComboBox combo_tipoUsuario;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelBotones;
    private javax.swing.JPanel panel_altaUsuario;
    private javax.swing.JPanel panel_consultaUsuario;
    private javax.swing.JPasswordField pfield_confirmaPassword;
    private javax.swing.JPasswordField pfield_password;
    private javax.swing.JTable tabla_usuarios;
    private javax.swing.JTextField tfield_nombreUsuario;
    // End of variables declaration//GEN-END:variables
}
